package com.crb.smartcard.cardutils;


import com.crb.smartcard.SmartCardConfig;
import com.crb.smartcard.cardutils.entity.BalanceAndCardNumberEntity;
import com.crb.smartcard.core.bean.CardResult;
import com.crb.smartcard.core.oma.SmartCard;
import com.crb.smartcard.utils.ApduUtil;
import com.crb.smartcard.utils.LogUtil;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * 获取卡余额
 * BalanceAndCardNumberEntity{
 * code='0000',
 * info='获取余额卡号成功',
 * balance='0',
 * cardNumber='3107060000000050473',
 * startDate='20240121',
 * endDate='20501231'}
 */
public class GetCardBalanceUtil {
    private static final String TAG = "GetCardBalanceUtil";

    public static BalanceAndCardNumberEntity getBalanceAndCardNumber(String aid) {
        String selectAid = ApduUtil.getSelectAid(aid);
        List<String> cApduList = new ArrayList<>();
        cApduList.add(selectAid);
        cApduList.add("00B095001E");
        cApduList.add("805C000204");
        List<CardResult> rApduList = new ArrayList<>();
        for (String str : cApduList) {
            CardResult execute = SmartCard.execute(str);
            LogUtil.e(TAG, execute.toString());
            rApduList.add(execute);
        }
        CardResult cardResult1 = rApduList.get(1);
        CardResult cardResult2 = rApduList.get(2);
        if (cardResult1.getStatus() == 0
                && !cardResult1.getRapdu().equals("")
                && cardResult2.getStatus() == 0
                && !cardResult2.getRapdu().equals("")) {
            String cardNumber = cardResult1.getRapdu().substring(21, 40);
            String startDate = cardResult1.getRapdu().substring(40, 48);
            String endDate = cardResult1.getRapdu().substring(48, 56);
            int walletBalance = new BigInteger(cardResult2.getRapdu(), 16).intValue();
            BalanceAndCardNumberEntity balanceAndCardNumberEntity = new BalanceAndCardNumberEntity();
            balanceAndCardNumberEntity.setCode(SmartCardConfig.SUCCESSFUL_CODE);
            balanceAndCardNumberEntity.setInfo("获取余额卡号成功");
            balanceAndCardNumberEntity.setBalance(String.valueOf(walletBalance));
            balanceAndCardNumberEntity.setCardNumber(cardNumber);
            balanceAndCardNumberEntity.setStartDate(startDate);
            balanceAndCardNumberEntity.setEndDate(endDate);
            LogUtil.e(TAG, balanceAndCardNumberEntity.toString());
//            String s = new Gson().toJson(balanceAndCardNumberEntity);
            SmartCard.closeChannel();
            return balanceAndCardNumberEntity;
        } else {
            BalanceAndCardNumberEntity balanceAndCardNumberEntity = new BalanceAndCardNumberEntity();
            balanceAndCardNumberEntity.setCode(SmartCardConfig.FAILURE_CODE);
            balanceAndCardNumberEntity.setInfo("获取余额卡号失败");
            LogUtil.e(TAG, balanceAndCardNumberEntity.toString());
//            String s = new Gson().toJson(balanceAndCardNumberEntity);
            SmartCard.closeChannel();
            return balanceAndCardNumberEntity;
        }
    }
}
